/*
 * Reward.cpp
 *
 *  Created on: 2013. 11. 1.
 *      Author: the2
 */

#include "Reward.h"
#include <sstream>

#include "../User.h"

namespace ProjectZ {
namespace NLogic {

Reward::Reward(User * pUser) : _user(pUser), _lastLoginTime(0), _clsRewardVector(RewardType::__MAX__)
{
	_user = pUser;
	for(int i = RewardType::DAILY_PET_REWARD; i < RewardType::__MAX__; i++)
	{
		_clsRewardVector[i] = new Data;
	}
}

Reward::~Reward()
{
	for(int i = RewardType::DAILY_PET_REWARD; i < RewardType::__MAX__; i++)
	{
		delete _clsRewardVector[i];
	}
}

void
Reward::Initialize()
{
	for(int i = RewardType::DAILY_PET_REWARD; i < RewardType::__MAX__; i++)
	{
		_clsRewardVector[i]->Clear();
	}
}

void
Reward::Finalize()
{
	for(int i = RewardType::DAILY_PET_REWARD; i < RewardType::__MAX__; i++)
	{
		_clsRewardVector[i]->Clear();
	}
}

Reward::Data *
Reward::GetData(int rewardType)
{
	return _clsRewardVector[rewardType];
}

void
Reward::load_Reward()
{
	{
		int index = 0;
		int DataIndex = 0;
		std::string strTemp;

		this->_clsRewardVector.clear();

		while (_user->GetUserInfo().reward[index] != '\0')
		{
			if (',' == _user->GetUserInfo().reward[index])
			{
				switch (DataIndex)
				{
				case RewardType::DAILY_PET_REWARD :
					break;
				case RewardType::FRIENDSHIP_BOX_REWARD :
					break;
				case RewardType::DAILY_PAIRY_REWARD :
					break;
				case RewardType::DAILY_ATTENDANCE_REWARD :
					this->GetData(DataIndex)->_value = atoll(strTemp.c_str());
					TESTLOG("load GetData DAILY_REWARD_LIST flag %d, userseq %d", this->GetData(DataIndex)->_value, this->_user->GetUserSeq());
					break;
				case RewardType::LAST_WEEK_RANK_REWARD :
					break;
				case RewardType::LAST_WEEK_RANK_FRIEND :
					break;
				case RewardType::CONNECT_EVENT :
					break;
				}
				strTemp.clear();
				index++;
				continue;
			}
			else if ('|' == _user->GetUserInfo().reward[index])
			{
				switch (DataIndex)
				{
				case RewardType::DAILY_PET_REWARD :
					this->GetData(DataIndex)->_value = atoll(strTemp.c_str());
					break;
				case RewardType::FRIENDSHIP_BOX_REWARD :
					this->GetData(DataIndex)->_value = atoll(strTemp.c_str());
					break;
				case RewardType::DAILY_PAIRY_REWARD :
					break;
				case RewardType::DAILY_ATTENDANCE_REWARD :
					this->GetData(DataIndex)->_subValue = atoll(strTemp.c_str());
					TESTLOG("load GetData DAILY_REWARD_LIST date %d userseq %d", this->GetData(DataIndex)->_subValue, this->_user->GetUserSeq());
					break;
				case RewardType::LAST_WEEK_RANK_REWARD :
					break;
				case RewardType::LAST_WEEK_RANK_FRIEND :
					break;
				case RewardType::CONNECT_EVENT :
					break;
				}
				strTemp.clear();
				index++;
				DataIndex++;
			}
			else
			{
				strTemp += _user->GetUserInfo().reward[index];
				index++;
			}
		}
	}
}

void
Reward::save_Reward()
{
	std::stringstream stream;
	for(int i = RewardType::DAILY_PET_REWARD; i < RewardType::__MAX__; i++)
	{
		switch (i)
		{
		case RewardType::DAILY_PET_REWARD :
			stream << this->GetRewardVector()[i]->_value;
			break;
		case RewardType::FRIENDSHIP_BOX_REWARD :
			stream << this->GetRewardVector()[i]->_value;
			break;
		case RewardType::DAILY_PAIRY_REWARD :
			break;
		case RewardType::DAILY_ATTENDANCE_REWARD :
			stream << this->GetRewardVector()[i]->_value;
			stream << ',';
			stream << this->GetRewardVector()[i]->_subValue;
			TESTLOG("save DAILY_REWARD_LIST flag %d, date %d, userseq %d", this->GetRewardVector()[i]->_value, this->GetRewardVector()[i]->_subValue, this->_user->GetUserSeq());
			break;
		case RewardType::LAST_WEEK_RANK_REWARD :
			break;
		case RewardType::LAST_WEEK_RANK_FRIEND :
			break;
		case RewardType::CONNECT_EVENT :
			break;
		}
		stream << "|";
	}

	_user->GetUserInfo().reward = stream.str().c_str();
}

//void
//Reward::SetLastLoginTime(time_t lastLoginTime)
//{
//	_lastLoginTime = lastLoginTime;
//	std::for_each(_clsRewardVector.begin(), _clsRewardVector.end(), [&](Data * vector) {
//		vector->_rewardReceiveTime = lastLoginTime;
//	});
//}

} /* namespace NLogic */
} /* namespace ProjectZ */
